<?php

class BUsers extends BaseModel
{
    protected $res;
    protected $key = "admin_";

    public function getSource()
    {
        return "f_b_users";
    }

    public function initialize()
    {
        parent::initialize();
    }

    public function details($where = '1=1', $columns = '*')
    {
        $data = [];
        $data = $this::findFirst(
            array(
                'conditions' => $where,
                "columns"    => $columns,
            )
        );
        return $data ? $data->toarray() : [];
    }

    /**
     * @func 本月总贡献
     * @return Mixed
     */
    public function contributionTotal()
    {
        $start = date("Y-m-01 00:00:00", time());
        $end   = date('Y-m-t 23:59:59', time());
        $where = 'created_at >= \'' . $start . '\' and created_at <= \'' . $end . '\'';
        $data  = $this::findFirst(
            array(
                'conditions' => $where,
                "columns"    => 'sum( contribution ) AS total',
            )
        );
        if ($data) {
            $data = $data->toarray();
        }
        return isset($data['total']) ? $data['total'] : 0;

    }


    /**
     * 获取上级们 id及等级
     * @param  $path
     * @return mixed
     */
    public function forefathers($path)
    {
        $where = '(' . $path . ')';
        $sql = <<<EOT
            SELECT
                u.id,u.level_id,b.cash_points
            FROM
                User AS u
                LEFT JOIN BUsers as b
                on u.id=b.user_id
            WHERE
                u.id in{$where}
               
EOT;

        /**
         * @see ManagerInterface::executeQuery
         */
        $result = $this->modelsManager->executeQuery($sql);
        $result = $result ? $result->toarray() : [];

        return $result;
    }


}
